package com.hbx.platform.auth.controller;

import com.hbx.platform.auth.Dto.RoleMenuDto;
import com.hbx.platform.auth.domain.RoleMenu;
import com.hbx.platform.auth.query.RoleMenuQuery;
import com.hbx.platform.auth.service.IRoleMenuService;
import com.hbx.platform.base.utils.AjaxResult;
import com.hbx.platform.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {
    @Autowired
    public IRoleMenuService roleMenuService;


    /**
     * 保存和修改公用的
     *
     * @param roleMenu 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody RoleMenu roleMenu) {
        try {
            if (roleMenu.getId() != null) {
                roleMenuService.update(roleMenu);
                return AjaxResult.me().setMessage("修改成功");
            } else {
                roleMenuService.save(roleMenu);
                return AjaxResult.me().setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错：" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            roleMenuService.delete(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            RoleMenu roleMenu = roleMenuService.queryById(id);
            return AjaxResult.me().setData(roleMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult getAll() {

        try {
            List<RoleMenu> list = roleMenuService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult page(@RequestBody RoleMenuQuery query) {
        try {
            PageList<RoleMenu> pageList = roleMenuService.page(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/handlerTree")
    public AjaxResult handlerTree(@RequestBody RoleMenuDto roleMenuDto){
        try {
            roleMenuService.deleteByRoleId(roleMenuDto.getRoleId());
            List<Long> menuIds = roleMenuDto.getMenuIds();
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleMenuDto.getRoleId());
                roleMenu.setMenuId(menuId);
                roleMenuService.save(roleMenu);
            }
            return AjaxResult.me().setMessage("添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("添加菜单失败");
        }
    }

    @GetMapping("/getMenus/{id}")
    public AjaxResult getMenu(@PathVariable("id") Long id){
        try {
            List<Long> list = roleMenuService.queryByRoleId(id);
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }

    }


}
